//*****************************************************************************
// Name : ftdNet.h
// Function : implement net function
// Author : forward
// Time : 2012-06-07
// Version : 1.0
//*****************************************************************************

#ifndef __FORTHEDREAM_NET__
#define __FORTHEDREAM_NET__

#include "curl/curl.h"
#include "SingleTon.h"

#include <vector>
namespace ftd
{
	class ftd_Net
	{
	public:
		ftd_Net(){};
		virtual ~ftd_Net(){};

		virtual FTDBOOL init(FTDSTRING initAddr="127.0.0.1",FTD_PROTOCOL_TYPE type=HTTP_POST) = 0;
		virtual FTDBOOL start() = 0;
		virtual FTDBOOL stop() = 0;
		virtual FTDBOOL send() = 0;
		virtual FTDBOOL receive() = 0;
		virtual FTDBOOL formatHead() = 0;
		virtual FTDBOOL parseHead() = 0;

		//////////////////////////////////////////////////////////////////////////
		FTDSTRING getLocalIP();		// get local IP information,implement later.
	protected:
		std::vector<FTDSTRING> mIPInfo;
	};

	class ftd_Server : public SingleTon<ftd_Server>,public ftd_Net
	{
	public:
		ftd_Server();
		~ftd_Server();

		FTDBOOL init(FTDSTRING initAddr="127.0.0.1",FTD_PROTOCOL_TYPE type=HTTP_POST);		// initialize the server
		FTDBOOL start();		// start the server
		FTDBOOL stop();		// stop the server
		FTDBOOL send();
		FTDBOOL receive();

	private:
		FTDBOOL formatHead();
		FTDBOOL parseHead();
	private:
		struct curl_slist *pHead;
		NetInfo netInfo;
	};

	//========================================================
	class ftd_Client : public SingleTon<ftd_Client>,public ftd_Net
	{
	public:
		ftd_Client();
		~ftd_Client();

		FTDBOOL init(FTDSTRING initAddr="127.0.0.1",FTD_PROTOCOL_TYPE type=HTTP_POST);		// initialize the client
		FTDBOOL start();		// start the client
		FTDBOOL stop();		// stop the client
		FTDBOOL send();
		FTDBOOL receive();
	private:
		FTDBOOL formatHead();
		FTDBOOL parseHead();
	private:
		CURL* mHandle;
		FTDCHAR szCookie[32];
		struct curl_slist *pHeadRequest;
		FTDINT mBufferLength;
		NetInfo netInfo;
	};
}
#endif